#!coding=utf-8
from standard_function import *


def chear_soft(dr):
    try:
        btn=WaitforElementById(dr,'com.mumayi.market.ui:id/img_xiazai_test',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()
    time.sleep(2)
    try:
        btn2=WaitforElementById(dr,'com.mumayi.market.ui:id/rb_2',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn2.click()
    time.sleep(2)
    while True:
        try:
            btns=WaitforElementsById(dr,'com.mumayi.market.ui:id/left',30)
        except NoSuchElementException:
            break
        else:
            btns[0].click()
            time.sleep(2)
            target_click(dr,329,638)
            time.sleep(2)
            try:
                enable_btn=WaitforElementById(dr,'com.mumayi.market.ui:id/mumayi_golden_eggs_dialog_button_3',30)
            except NoSuchElementException:
                continue
            else:
                enable_btn.click()

def clickappdown(driver):
    try:
        down_btns=WaitforElementsById(driver,'com.mumayi.market.ui:id/baseadapter_item_bg',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        print len(down_btns)
        for item in down_btns:
            try:
                size=WaitforElementById(item,"com.mumayi.market.ui:id/tv_item_mess",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size.split(',')[0].strip()
            if 'G' not in size:
                if 'K' in size or float(size.split(',')[0].strip()[0:-1])<10:
                    continue
            try:
                btn=WaitforElementById(item,'com.mumayi.market.ui:id/ldb_btn',5)
            except NoSuchElementException:
                continue
            btn.click()
            waittime=20
            while waittime:
                try:
                    text=WaitforElementById(item,'com.mumayi.market.ui:id/tv_down_sign',5).get_attribute("text")
                except NoSuchElementException:
                    pass
                print text
                if '0%' not in text:
                    btn.click()
                    break
                else:
                    waittime=waittime-1
                    time.sleep(0.5)
                    if waittime==0:
                        btn.click()
                        break
def soft(dr):
    categorys =[u'系统工具',u'主题壁纸',u'网络预览',u'即时通讯',u'社交微博',u'音乐视频',u'摄影图像',u'天气时间',u'安全杀毒',u'新闻资讯',u'通话增强',u'短信增强', \
                u'便捷生活',u'出行地图',u'购物理财',u'教育学校',u'商务办公',u'医疗保健',u'休闲娱乐',u'图书动漫']
    app_btn=''
    try:
        app_btn=WaitforElementById(dr,'com.mumayi.market.ui:id/app',30)
    except:
        raise  NoSuchElementException
    else:
        app_btn.click()
    time.sleep(2)
    try:
        btn_1=WaitforElementById(dr,'com.mumayi.market.ui:id/rb_4',30)
    except:
        raise  NoSuchElementException
    else:
        btn_1.click()

    try:
        error_btn=WaitforElementById(dr,'com.mumayi.market.ui:id/text_error',5)
    except NoSuchElementException:
        pass
    else:
        error_btn.click()

    time.sleep(2)
    swipeUp(dr,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for name in categorys:
        print "Current categorys is:",name
        try:
            btn=WaitforElementByName(dr,name,30)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            time.sleep(1)
            try:
                clickappdown(dr)
                dr.back()
            except NoSuchElementException:
                dr.back()
                continue
        app_btn.click()




def game(dr):
   pass
def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION             # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.mumayi.market.ui'    # apk的包名
    desired_caps['appActivity'] = '.MainFrameActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver= webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    chear_soft(driver)
    soft(driver)
    game(driver)
    driver.quit()

#run()